﻿using Microsoft.AspNetCore.Http;
using Models.Common;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using UtilsORM;
using UtilsORM.MySqlData;

////using System.Web.Script.Serialization;

namespace GatewayServerService.Data
{
    public class DBIotDeviceTaskPlainItem
    {

        EFYihongIotContext ef = new EFYihongIotContext();
        public iot_device_task_plain_item getPlainItem(string guid)
        {
            return ef.iot_device_task_plain_item.Where(c => c.tpi_guid == guid).FirstOrDefault();
        }

        public List<iot_device_task_plain_item> getPlainItemList(string tpguid)
        {
            return ef.iot_device_task_plain_item.Where(c => c.tpi_tp_guid == tpguid).OrderBy(c => c.tpi_order).ToList();
        }
        public void delItem(string guid)
        {
            iot_device_task_plain_item item = ef.iot_device_task_plain_item.Where(c => c.tpi_guid == guid).FirstOrDefault();
            ef.iot_device_task_plain_item.Remove(item);
            ef.SaveChanges();
        }

        public bool saveAll(IFormCollection FC, HttpReqModel httpRequest, MagerLoginInfo LoginUserInfo, iot_device_task_plain_item req)
        {

            //先查一下有没重复提交
            iot_device_task_plain_item model = ef.iot_device_task_plain_item.Where(c => c.tpi_guid == req.tpi_guid).FirstOrDefault();
            if (model == null)
            {

                //写入
                req.tpi_add_time = DateTime.Now;
                req.tpi_mod_time = DateTime.Now;
                req.tpi_complete = 1;

                ef.iot_device_task_plain_item.Add(req);
                ef.SaveChanges();
            }
            else
            {
                model.tpi_title = req.tpi_title;
                model.tpi_status = req.tpi_status;
                model.tpi_taskobj_guid = req.tpi_taskobj_guid;
                model.tpi_type = req.tpi_type;
                model.tpi_key = req.tpi_key;
                model.tpi_value = req.tpi_value;
                model.tpi_value_rlue = req.tpi_value_rlue;

                model.tpi_order = req.tpi_order;
                model.tpi_repeat_count = req.tpi_repeat_count;
                model.tpi_repeat_interval = req.tpi_repeat_interval;
                //model.ti_repeat_count = req.ti_repeat_count;
                model.tpi_mod_time = DateTime.Now;
                ef.iot_device_task_plain_item.Update(model);
                ef.SaveChanges();
            }

            return true;
        }

    }
}
